package com.cjb.cloudsystem.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cjb.cloudfabric.util.PDFUtil;
import com.cjb.cloudhdfs.api.HadoopUtil;
import com.cjb.cloudsystem.entity.FabricBasicAsset;
import com.cjb.cloudsystem.service.FabricBasicAssetService;
import com.cjb.cloudsystem.vo.FabricBasicAssetVO;
import com.cjb.common.util.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.Path;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.*;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static com.cjb.common.util.WebUtil.previewPDF;

/**
* @Author 1.0.0 2023-10-30
* @Package IntelliJ IDEA
* @Class FabricBasicAssetControl
* @Date 2023-10-30
*/
@RestController
@Tag(name="fabricBasicAsset")
@RequestMapping(value = {"fabricBasicAsset"})
@ApiResponses(@ApiResponse(responseCode = "200", description = "接口请求成功"))
@Slf4j
public class FabricBasicAssetController {
    /*
    * 注入事物类
    */
    @Autowired
    private FabricBasicAssetService fabricBasicAssetService;
    /*
     * 注入事物类
     */
    @Autowired
    private HadoopUtil hadoopUtil;
    /**
    *单个保存
    *
    * @Author 1.0.0 2023-10-30
    * @Value
    * @Title
    */
    @PostMapping("/save")
    @Operation(summary = "单个保存")
    public Result<String> save(@RequestBody FabricBasicAsset fabricBasicAsset){
        return Result.success(fabricBasicAssetService.saveBasic(fabricBasicAsset));
    }

    /**
    *批量保存
    *
    * @Author 1.0.0 2023-10-30
    * @Value
    * @Title
    */
    @PostMapping("/listSave")
    @Operation(summary = "批量保存")
    public Result <Integer> listSave(@RequestBody List<FabricBasicAsset> fabricBasicAsset){
        return Result.success(fabricBasicAssetService.listSave(fabricBasicAsset));
    }

    /**
    *单个查询
    *
    * @Author 1.0.0 2023-10-30
    * @Value
    * @Title
    */
    @GetMapping("/queryById")
    @Operation(summary = "单个查询-通过ID")
    public Result<FabricBasicAsset> queryById(@RequestBody FabricBasicAsset fabricBasicAsset){
        return Result.success( fabricBasicAssetService.queryById(fabricBasicAsset));
    }

    /**
    * 分页查询
    *
    * @Author 1.0.0 2023-10-30
    * @Value
    * @Title
    */
    @GetMapping("/queryPage")
    @Operation(summary = "分页查询-通过页码")
    public Result<Page<FabricBasicAsset>> queryPage(@ParameterObject @Valid FabricBasicAssetVO fabricBasicAssetVO){
        log.info("打印传入对象 {}", JsonUtils.toJsonString(fabricBasicAssetVO));
        Page fabricBasicAssetPage;
        if (fabricBasicAssetVO!=null&& Common.isNotEmpty(fabricBasicAssetVO.getPage()) && Common.isNotEmpty(fabricBasicAssetVO.getLimit())){
            fabricBasicAssetPage = new Page<FabricBasicAsset>(fabricBasicAssetVO.getPage(),fabricBasicAssetVO.getLimit());
        }
        else {
            fabricBasicAssetPage = new Page<FabricBasicAsset>();
        }
        return Result.success(fabricBasicAssetService.queryPage(fabricBasicAssetPage,fabricBasicAssetVO));
    }

    /**
    * 批量更新
    *
    * @Author 1.0.0 2023-10-30
    * @Value
    * @Title
    */
    @PostMapping("/listUpdate")
    @Operation(summary = "批量更新")
    public Result <String> listUpdate(@RequestBody List<FabricBasicAsset> fabricBasicAsset){
        fabricBasicAssetService.listUpdate(fabricBasicAsset);
        return Result.success();
    }


    /**
    * 批量删除
    *
    * @Author 1.0.0 2023-10-30
    * @Value
    * @Title
    */
    @PostMapping("/listDelete")
    @Operation(summary = "批量删除")
    public Result <String> listDelete(@RequestBody List<FabricBasicAsset> fabricBasicAsset){
        fabricBasicAssetService.listDelete(fabricBasicAsset);
        return Result.success();
    }
    /**
    * 通过id删除
    *
    * @Author 1.0.0 2023-10-30
    * @Value
    * @Title
    */
    @PostMapping("/deleteById")
    @Operation(summary = "单个删除-通过ID")
    public Result <String> deleteById(@RequestBody FabricBasicAsset fabricBasicAsset){
        fabricBasicAssetService.deleteById(fabricBasicAsset);
        return Result.success();
    }

    /**
    * 返回全部信息
    *
    * @Author 1.0.0 2023-10-30
    * @Value
    * @Title
    */
    @PostMapping("/returnAllData")
    @Operation(summary = "返回所有信息")
    public Result <List<FabricBasicAsset>> returnAllData(){
        return Result.success(fabricBasicAssetService.returnAllData());
    }

    /**
     * 证书生成
     *
     * @Author chenjinbin
     * @Value
     * @Title
     */
    @GetMapping("downloadCertificate")
    @Operation(summary = "证书生成")
    public void downloadCertificate(String path) throws Exception {
        //读取file
        log.info("打印传入的路径 {}",JsonUtils.toJsonString(path));
        StringBuilder sb = new StringBuilder();
        BufferedReader br;
        try {
            br = new BufferedReader(new InputStreamReader(hadoopUtil.open(new Path(path))));
            String line;

            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
            br.close();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        Pattern pattern = Pattern.compile("-----BEGIN CERTIFICATE-----\\s*(.*?)\\s*-----END CERTIFICATE-----", Pattern.DOTALL);
        Matcher matcher = pattern.matcher(sb);
        if (matcher.find()){
            byte[] pdf = PDFUtil.createPdf(String.valueOf(matcher.group(1)));
            WebUtil.downloadByte(pdf, HttpContextUtils.getHttpServletResponse(),path);
        }
    }

}
